<template>
	<view class="page">
		<!-- #ifdef H5 -->
			<topVue title="会员等级购买" rightIcon="server-fill" :isBack="true"></topVue>
		<!-- #endif -->

		<view class="main-content">
			<scroll-view class="content" scroll-y>
				<view class="section">
					<text>当前会员级别</text>
					<view class="vip">{{user.membershipName?user.membershipName:'基础会员'}}</view>
					<view class="limit" v-if="user.membershipEndTime">有效期至{{user.membershipEndTime}}，新增不补差赠送累计。</view>
					<view class="li margin20 relative" v-for="(item, index) in list" :key="index"
						:class="{active: selectObj.id==item.id}" @click="selectFun(item)">
						<view class="flex align-center">
							<image src="../../static/radio1.png" style="width: 48rpx; height: 48rpx;" v-if="selectObj.id==item.id"></image>
							<image src="../../static/radio0.png" style="width: 48rpx; height: 48rpx;" v-else></image>
							<view class="name">{{item.title.split("-")[0]}}-</view>
						</view>
						<view class="name" style="padding-left: 58rpx;">{{ item.price }}元/年</view>
						<view class="descri">{{ item.description }}</view>
						<view class="price absolute">{{ item.price }}元/年</view>
					</view>
				</view>
				<view class="btn margin30" @click="buyFun()">立即购买</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
import { mapState } from "vuex"
	export default {
		data(){
			return {
				list: [],
				selectObj: {
					id: "",
					price: 0
				},
			}
		},
    computed: {
      ...mapState({
        user: state => state.user,
        isLogin: state => state.isLogin
      })
    },
		async onLoad() {
			await this.getList()
		},
		methods: {
			// 会员列表
			async getList(){
				const res = await this.$request("/aiet-user-center/user/membership/all", {}, "GET")
				if(res.code===200) this.list = res.data
			},

			// 选择套餐
			selectFun(item){
				this.selectObj = item
				console.log(this.selectObj)
			},

			// 立即购买
			async buyFun(){
				if(!this.selectObj.id) return this.$u.toast("请选择会员等级")
				const res = await this.$request("/aiet-user-center/user/orders/pay", {
					productName: this.selectObj.title,
					orderAmount: this.selectObj.price,
					platform: "数字人-H5",
					productType: 2,
					membershipId: this.selectObj.id,
					manufacturer:1
				}, "POST", "application/json")
				if(res.code===200) {
					console.log(res.data.payUrl)
					// #ifdef H5
						window.location.href = res.data.payUrl
					// #endif
				}else{
					this.$u.toast(res.msg)
				}
			}
		}
	}
</script>

<style lang="less" scoped>
	.main-content {
		width: 100%;
		height: calc(100vh - var(--window-bottom) - var(--window-top) - 92rpx);
		padding: 30rpx;
		box-sizing: border-box;
		background: linear-gradient(to bottom, #10B981, #3B82F6);
		.content {
			width: 100%;
			height: 100%;
			background: #fff;
			border-radius: 12rpx;
			padding: 30rpx;
			box-sizing: border-box;
			.section {
				width: 100%;
				background: #fff;
				border-radius: 10rpx;
				.vip {
					font-size: 32rpx;
					font-weight: 600;
					padding: 10rpx 0 20rpx 0;
					color: #4373EC;
				}
				.limit {
					color: #AEB1C4;
					padding-bottom: 20rpx;
				}
				.li {
					width: 100%;
					height: 240rpx;
					border-radius: 12rpx;
					background: #F9FAFB;
					padding: 30rpx;
					box-sizing: border-box;
					.price {
						width: 180rpx;
						height: 66rpx;
						line-height: 66rpx;
						text-align: center;
						background: #DCFCE7;
						color: #2AAF6F;
						top: 0rpx;
						right: 0rpx;
						font-weight: 600;
						border-radius: 0 12rpx 0 0;
					}
					.name {
						font-weight: 600;
						color: #000;
						padding-left: 10rpx;
					}
					.descri {
						color: #AEB1C4;
						padding: 10rpx 0 0 58rpx;
					}
				}
				.active {
					background: #F0FDF4;
					border: 1rpx solid #59D486;
				}
			}
			.btn {
				width: 100%;
				height: 88rpx;
				background: linear-gradient(to right, #10B981, #3B82F6);
				border-radius: 10rpx;
				color: #fff;
				lighting-color: 88rpx;
				text-align: center;
			}
		}
	}



</style>
